package org.alwayssuper.protocol.commons;

import io.netty.buffer.ByteBuf;

public class JTUtils {

    /**
     * BCC校验(异或校验)
     */
    public static byte bcc(ByteBuf byteBuf, int tailOffset) {
        byte cs = 0;
        int readerIndex = byteBuf.readerIndex();
        int writerIndex = byteBuf.writerIndex() + tailOffset;
        while (readerIndex < writerIndex)
            cs ^= byteBuf.getByte(readerIndex++);
        return cs;
    }

    /**
     * BCC校验(异或校验) - 重载方法，允许指定起始字节偏移量
     */
    public static byte bcc(ByteBuf byteBuf, int startOffset, int tailOffset) {
        byte cs = 0;
        int readerIndex = byteBuf.readerIndex() + startOffset;
        int writerIndex = byteBuf.writerIndex() + tailOffset;
        while (readerIndex < writerIndex)
            cs ^= byteBuf.getByte(readerIndex++);
        return cs;
    }

    /**
     * 根据版本返回消息头长度
     */
    public static int headerLength(int version) {
        return 22;
    }
}